#include "XParser.h"
#include <rmxftmpl.h>
#include <rmxfguid.h>
#include <d3dx9xof.h>
XParser::XParser(void)
{
}

XParser::~XParser(void)
{
}


bool XParser::Parse(LPCSTR fileName, void **data )
{
	ID3DXFile * pFile;
	ID3DXFileEnumObject * pEnumObj;
	ID3DXFileData  *pFileData;
	SIZE_T num = 0;

	HRESULT hr;
	if(FAILED(D3DXFileCreate(&pFile)))return false;
	if(FAILED(pFile->RegisterTemplates((LPVOID) D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES))) return false;
	if(FAILED(hr = pFile->CreateEnumObject((LPVOID)fileName, D3DXF_FILELOAD_FROMFILE, &pEnumObj))) return false;
	if(FAILED(pEnumObj->GetChildren(&num)))return false;
	for(int i = 0; i < num; i++)
	{
		if(SUCCEEDED(pEnumObj->GetChild(i, &pFileData)))
		{
			ParseObject(pFileData, NULL, 0, data, false);
			pFileData->Release();
		}
	}
	pEnumObj->Release();
	pFile->Release();
	return true;
}

bool XParser::ParseChildObject(ID3DXFileData *obj, DWORD depth, void **data, bool ref)
{
	if(ref) return false;
	++depth;

	ID3DXFileData *pData;
	SIZE_T num;

	if(FAILED(obj->GetChildren(&num))) return false;
	for(int i = 0; i < num; i++)
	{
		if(SUCCEEDED(obj->GetChild(i, &pData)))
		{
			ParseObject(pData, obj, depth, data, pData->IsReference());
			pData->Release();
		}
	}
	return true;
}